package com.sgcc.fj.pmos.e_fj_order.utils;

import android.graphics.Color;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.sgcc.fj.pmos.common.utils.BigDecimalUtils;
import com.sgcc.fj.pmos.common.utils.DateUtils;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.fj.pmos.e_fj_order.bean.PackageInfoBean;
import com.sgcc.fj.pmos.e_fj_order.bean.PackageOrdersBean;
import com.sgcc.fj.pmos.e_fj_order.bean.PeriodPointBean;
import com.sgcc.fj.pmos.e_fj_order.bean.ResultBargainBean;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class OrderDetailsUtils {

    public static boolean getIsSame(List<ResultBargainBean> resultBargainBeanList,
                                    List<ResultBargainBean> resultBargainBeanList_new,
                                    String type) {
        if (resultBargainBeanList != null && resultBargainBeanList_new != null) {
            if (resultBargainBeanList.size() == 24 && resultBargainBeanList_new.size() == 24) {
                for (int i = 0; i < resultBargainBeanList_new.size(); i++) {
                    if ("1".equals(type)) {
                        if (!resultBargainBeanList.get(i).geteItemValue().equals(resultBargainBeanList_new.get(i).geteItemValue())) {
                            return true;
                        }
                    }
                    if ("2".equals(type)) {
                        if (!resultBargainBeanList.get(i).getPlItemValue().equals(resultBargainBeanList_new.get(i).getPlItemValue())) {
                            return true;
                        }
                    }
                    if ("3".equals(type)) {
                        if (!resultBargainBeanList.get(i).getNdItemValue().equals(resultBargainBeanList_new.get(i).getNdItemValue())) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public static void setViewData(TextView textView, String type, String itemId, String peType,
                                   String itemName, String itemValue, String itemName_new, String itemValue_new, List<ResultBargainBean> resultBargainBeanList,
                                   List<ResultBargainBean> resultBargainBeanList_new) {
        if ("D111".equals(itemId)) {
            if ("01".equals(peType) || "02".equals(peType)) {
                textView.setText(itemName);
                boolean iSame = getIsSame(resultBargainBeanList, resultBargainBeanList_new, type);
                if (iSame) {
                    textView.setTextColor(Color.parseColor("#FFA500"));
                } else {
                    textView.setTextColor(Color.parseColor("#0092FF"));
                }

                textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (resultBargainBeanList_new != null) {
                            List<String> list = new ArrayList<>();
                            for (ResultBargainBean bean : resultBargainBeanList) {
                                if ("1".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.geteItemValue()));
                                }
                                if ("2".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getPlItemValue()));
                                }
                                if ("3".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getNdItemValue()));
                                }

                            }

                            List<String> list_new = new ArrayList<>();
                            for (ResultBargainBean bean : resultBargainBeanList_new) {
                                if ("1".equals(type)) {
                                    list_new.add(BigDecimalUtils.onKeep5Decimal_divide(bean.geteItemValue()));
                                }
                                if ("2".equals(type)) {
                                    list_new.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getPlItemValue()));
                                }
                                if ("3".equals(type)) {
                                    list_new.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getNdItemValue()));
                                }
                            }
                            DialogUtil.showLineChartTwoLineDialog(ActivityUtils.getTopActivity(), list_new, list);
                        } else {
                            List<String> list = new ArrayList<>();
                            for (ResultBargainBean bean : resultBargainBeanList) {
                                if ("1".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.geteItemValue()));
                                }
                                if ("2".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getPlItemValue()));
                                }
                                if ("3".equals(type)) {
                                    list.add(BigDecimalUtils.onKeep5Decimal_divide(bean.getNdItemValue()));
                                }

                            }
                            DialogUtil.showLineChartDialog(ActivityUtils.getTopActivity(), list);
                        }

                    }
                });
            } else {
                if (TextUtils.isEmpty(itemValue_new)) {
                    textView.setTextColor(Color.parseColor("#464646"));
                    textView.setText("约定价格:" + BigDecimalUtils.onKeep5Decimal_divide(itemValue) + "元/度");
                } else {

                    String oldstr = "约定价格:" + BigDecimalUtils.onKeep5Decimal_divide(itemValue) + "元/度";
                    String oldnew = "约定价格:" + BigDecimalUtils.onKeep5Decimal_divide(itemValue_new) + "元/度";

//                    if (!oldstr.equals(oldnew)) {
//                        textView.setTextColor(Color.parseColor("#464646"));
//                    } else {
//                        textView.setTextColor(Color.parseColor("#FFA500"));
//                    }

                    textView.setText("约定价格:" + BigDecimalUtils.onKeep5Decimal_divide(itemValue) + "元/度");
                    if (!oldstr.equals(oldnew)) {
                        SpannableString span = new SpannableString(textView.getText().toString());
                        span.setSpan(new ForegroundColorSpan(Color.parseColor("#FFA500")),
                                5, textView.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        textView.setText(span);
                    }


                }
            }
        } else {
            if (TextUtils.isEmpty(itemName_new)) {
                textView.setText(itemName);
                textView.setTextColor(Color.parseColor("#464646"));
            } else {
                if (itemName.equals(itemName_new)) {
                    textView.setTextColor(Color.parseColor("#464646"));
                } else {
                    textView.setTextColor(Color.parseColor("#FFA500"));
                }
                textView.setText(itemName);
            }
        }
    }

    /**
     * 根据当前月份在约定电量里找对应月份的bean
     *
     * @param month
     * @return
     */
    public static PeriodPointBean getBeanByEnergyByMonthDetailList(String month, List<PeriodPointBean> mEnergyByMonthDetailList) {
        for (PeriodPointBean dataBean : mEnergyByMonthDetailList) {
            String monthDate = dataBean.getMonthDate();
            if (!TextUtils.isEmpty(monthDate)) {
                String month1 = monthDate.substring(5, 7);
                if (month.contains("月")) {
                    month = month.replace("月", "");
                }
                if (month.equals(month1)) {
                    return dataBean;
                }
            }
        }
        return null;
    }

    /**
     * 根据月份在合约下拉接口里找对应的bean
     *
     * @param month
     * @return
     */
    public static PackageOrdersBean.DataBean getPackageOrdersBean(String month, List<PackageOrdersBean.DataBean> mPackageOrdersBeanList) {
        for (PackageOrdersBean.DataBean dataBean : mPackageOrdersBeanList) {
            String monthDate = DateUtils.getStrng_to_Times_ymd_4_1(dataBean.getMonthDate());
            if (month.contains("月")) {
                month = month.replace("月", "");
            }
            if (month.equals(monthDate)) {
                return dataBean;
            }
        }
        return null;
    }

    public static String getPackageId(String month, List<PackageInfoBean.DataDTO> mPackageInfoList) {
        String result = "";
        for (PackageInfoBean.DataDTO dataDTO : mPackageInfoList) {
            String monthDate = dataDTO.getMonthDate();
            if (!TextUtils.isEmpty(monthDate)) {
                String month1 = DateUtils.getStrng_to_Times_ymd_4_1(monthDate);
                if (month.contains("月")) {
                    month = month.replace("月", "");
                }
                if (month.equals(month1)) {
                    result = dataDTO.getPackageId();
                }
            }
        }
        return result;
    }

    public static String getIdbyPackageInfo(String month, boolean isMain, List<PackageInfoBean.DataDTO> mPackageInfoList) {
        String result = "";
        for (PackageInfoBean.DataDTO dataDTO : mPackageInfoList) {
            String monthDate = dataDTO.getMonthDate();
            if (!TextUtils.isEmpty(monthDate)) {
                String month1 = DateUtils.getStrng_to_Times_ymd_4_1(monthDate);
                if (month.contains("月")) {
                    month = month.replace("月", "");
                }
                if (month.equals(month1)) {
                    if (isMain) {
                        result = dataDTO.getMainSchemeId();
                    } else {
                        result = dataDTO.getFloSchemeId();
                    }
                }

            }
        }
        return result;
    }

    public static PackageInfoBean.DataDTO getBeanByPackageInfo(String month, List<PackageInfoBean.DataDTO> mPackageInfoList) {
        for (PackageInfoBean.DataDTO dataDTO : mPackageInfoList) {
            String monthDate = dataDTO.getMonthDate();
            if (!TextUtils.isEmpty(monthDate)) {
                String month1 = DateUtils.getStrng_to_Times_ymd_4_1(monthDate);
                if (month.contains("月")) {
                    month = month.replace("月", "");
                }
                if (month.equals(month1)) {
                    return dataDTO;
                }
            }
        }
        return null;
    }

    public static List<String> getFieldValue(Object o) {
        try {
            List<String> list = new ArrayList<>();
            String getter = "get" + "Energy";
            for (int i = 1; i < 25; i++) {
                String s1 = getter + String.valueOf(i);
                Method method = o.getClass().getMethod(s1, new Class[]{});
                Object value = method.invoke(o, new Object[]{});
                list.add((String) value);
            }
            return list;
        } catch (Exception e) {

        }
        return null;
    }


    public static String status_left(String modificationType) {
        String result = "";
        switch (modificationType) {
            case "201":
                result = "合约建立";
                break;
            case "202":
                result = "电量变更";
                break;
            case "203":
                result = "议价";
                break;
            case "204":
                result = "合约解除";
                break;
            case "205":
                result = "电价变更";
                break;
            case "206":
                result = "备案补充协议";
                break;
        }
        return result;
    }

    public static String status_right(String mStatus) {
        String result = "";
        switch (mStatus) {
            case "101":
                result = "待用户确认";
                break;
            case "102":
                result = "待售电公司确认";
                break;
            case "103":
                result = "用户已确认";
                break;
            case "104":
                result = "售电公司已确认";
                break;
            case "105":
                result = "用户已驳回";
                break;
            case "106":
                result = "售电公司已驳回";
                break;
            case "107":
                result = "用户已撤回";
                break;
            case "108":
                result = "售电公司已撤回";
                break;
        }
        return result;
    }

    /**
     * @param mainType 基准套餐：分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
     * @param floType  浮动套餐：分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
     * @return 分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
     */
    public static String getGoodsType(String mainType, String floType) {
        boolean isfenshiduan = false;
        boolean isfendianliang = false;
        if (("01".equals(mainType) || "02".equals(mainType)) || ("01".equals(floType) || "02".equals(floType))) {
            isfenshiduan = true;
        }

        if (("01".equals(mainType) || "03".equals(mainType)) || ("01".equals(floType) || "03".equals(floType))) {
            isfendianliang = true;
        }

        if (isfenshiduan && isfendianliang) {
            return "01";
        } else if (!isfenshiduan && !isfendianliang) {
            return "04";
        } else if (isfenshiduan) {
            return "02";
        } else if (isfendianliang) {
            return "03";
        }
        return "";
    }

}
